#include "genMips.h"

void initGenMips ()
{
	nbLabelPourGenMips = 0 ;
}

void mipsGeneration(FILE * out)
{
	dataGeneration(out);
	textGeneration(out);
}

void dataGeneration (FILE * out)
{
	int i;
	//char * clef;
	//entree element;
	//pChaineChaineEntreeNode ligne;
	
	fprintf(out,".data\n");
	fprintf(out,"\tsys_retour_ligne:	.asciiz	\"\\n\"\n");
	fprintf(out,"\tsys_vrai:	.asciiz	\"vrai\"\n");
	fprintf(out,"\tsys_faux:	.asciiz	\"faux\"\n");
	
	/*
	for(i=0; i<HASH_TABLE_SIZE; i++)
	{
		if(existeIndexHashTable(i))
		{
			ligne = recupLigneHashTable(i);
			while(!chaineChaineEntreeNodeEstNull(ligne))
			{
				if(cibleChaineChaineEntreeNode(ligne) != NULL)
				{
					clef = clefChaineChaineEntreeNode(ligne);
					element = consulteHashTable(clef);
					
					if(element.typeEntree == ENTREE_TYPEENTREE_CTE)
					{
						if(element.valeur.type == ENTREE_TYPECTE_ENTIER)
						{
							fprintf(out,"%s:	.word	%d\n",clef,element.valeur.valeurIntBool);
						}
						else if(element.valeur.type == ENTREE_TYPECTE_CHAR)
						{
							fprintf(out,"%s:	.asciiz	\"%s\"",clef,element.valeur.valeurChar);						
						}
						else
						{
							printf("Erreur : dataGeneration : type entree constante incorrect.\n");
							exit(-1);
						}
					}
				}
				else
				{
					fprintf(out,"%s:	.word\n",clef);
				}
				
				ligne = nextChaineChaineEntreeNode(clefChaineChaineEntreeNode(ligne));
			}
		}
	}
	*/
	
	for(i=0;i<nbVariableADeclarer;i++)
	{
		fprintf(out,"%s",varaibleADeclarer[i]);	
	}
	
	fprintf(out,"\n");
}

void codeEtOuNonXorGeneration(FILE * out)
{
	int x = nbLabelPourGenMips;
	
	fprintf(out,"_SysLabelOu : 	#$v1 :=  $a0 or $a1\n");
	fprintf(out,"\tmove $t0, $a0\n");
	fprintf(out,"\tmove $t1, $a1\n");
	fprintf(out,"\tli $t2, 1\n");
	fprintf(out,"\tbeq $t0, $t2, _SysLabelGenMips%d\n",x);
	fprintf(out,"\tbeq $t1, $t2, _SysLabelGenMips%d\n",x);
	fprintf(out,"\tli $t0, 0\n");
	fprintf(out,"\tb _SysLabelGenMips%d\n",x+1);
	fprintf(out,"\t_SysLabelGenMips%d :\n",x);
	fprintf(out,"\tli $t0, 1\n");
	fprintf(out,"\t_SysLabelGenMips%d :\n",x+1);
	fprintf(out,"\tmove $v1, $t0\n");
	fprintf(out,"\tjr $ra\n");
	fprintf(out,"\n");
	
	nbLabelPourGenMips += 2;
	
	x = nbLabelPourGenMips;
	
	fprintf(out,"_SysLabelEt :	#$v1 :=  $a0 and $a1\n");
	fprintf(out,"\tmove $t0, $a0\n");
	fprintf(out,"\tmove $t1, $a1\n");
	fprintf(out,"\tbeqz $t0, _SysLabelGenMips%d\n",x);
	fprintf(out,"\tbeqz $t1, _SysLabelGenMips%d\n",x);
	fprintf(out,"\tli $t0, 1\n");
	fprintf(out,"\tb _SysLabelGenMips%d\n",x+1);
	fprintf(out,"\t_SysLabelGenMips%d :\n",x);
	fprintf(out,"\tli $t0, 0\n");
	fprintf(out,"\t_SysLabelGenMips%d :\n",x+1);
	fprintf(out,"\tmove $v1, $t0\n");
	fprintf(out,"\tjr $ra\n");
	fprintf(out,"\n");
	
	nbLabelPourGenMips += 2;
	
	x = nbLabelPourGenMips;
	
	fprintf(out,"_SysLabelNon :	#$v1 :=  not $a0\n");
	fprintf(out,"\tmove $t0, $a0\n");
	fprintf(out,"\tbeqz $t0, _SysLabelGenMips%d\n",x);
	fprintf(out,"\tli $t0, 0\n");
	fprintf(out,"\tb _SysLabelGenMips%d\n",x+1);
	fprintf(out,"\t_SysLabelGenMips%d :\n",x);
	fprintf(out,"\tli $t0, 1\n");
	fprintf(out,"\t_SysLabelGenMips%d :\n",x+1);
	fprintf(out,"\tmove $v1, $t0\n");
	fprintf(out,"\tjr $ra\n");
	fprintf(out,"\n");
	
	nbLabelPourGenMips += 2;
	
	x = nbLabelPourGenMips;
	
	fprintf(out,"_SysLabelXor :	#$v1 :=  $a0 xor $a1\n");
	fprintf(out,"\tmove $t0, $a0\n");
	fprintf(out,"\tmove $t1, $a1\n");
	fprintf(out,"\tbeqz $t0, _SysLabelGenMips%d\n",x);
	fprintf(out,"\tbeqz $t1, _SysLabelGenMips%d\n",x+1);
	fprintf(out,"\tb _SysLabelGenMips%d\n",x+2);
	fprintf(out,"\t_SysLabelGenMips%d :\n",x);
	fprintf(out,"\tbeqz $t1, _SysLabelGenMips%d\n",x+2);
	fprintf(out,"\t_SysLabelGenMips%d :\n",x+1);
	fprintf(out,"\tli $t2, 1\n");
	fprintf(out,"\tb _SysLabelGenMips%d\n",x+3);
	fprintf(out,"\t_SysLabelGenMips%d :\n",x+2);
	fprintf(out,"\tli $t2, 0\n");
	fprintf(out,"\t_SysLabelGenMips%d :\n",x+3);
	fprintf(out,"\tmove $v1, $t2\n");
	fprintf(out,"\tjr $ra\n");
	fprintf(out,"\n");
	
	nbLabelPourGenMips += 4;
}

int functionGeneration(FILE * out, int debut)
{
	char * label = tableGencode[debut]->adresse1;
	
	fprintf(out,"%s :		#fonction %s\n",label,label);
	fprintf(out,"\n");	
	debut++;
	
	while(debut<nbElementGencode && (strcmp(tableGencode[debut]->commande,"finFonction") != 0))
	{
		parsingInstruction(out, debut);
		debut++;
	}
	fprintf(out,"\tjr $ra	#fin de la fonction %s\n",label);
	fprintf(out,"\n");	
	debut++;
	
	return debut;
}

void textGeneration (FILE * out)
{
	int i;
	int debut = 0;
	
	fprintf(out,".text\n.globl main\n\n");
	
	//génération des fonctions ou, et, xor et non
	codeEtOuNonXorGeneration(out);
	
	//génération des fonctions utilisateur
	while(debut<nbElementGencode && (strcmp(tableGencode[debut]->commande,"fonction") == 0))
	{
		debut = functionGeneration(out, debut);
	}
	
	fprintf(out,"\nmain :\n");
	
	//création de la pile
	fprintf(out,"\tli $s0,%d\n",maxPointeurPourGestionPile);
	fprintf(out,"\tsub $sp, $sp, $s0\n");
	fprintf(out,"\n");
	
	//génération du code
	for(i=debut;i<nbElementGencode;i++)
	{
		parsingInstruction(out, i);
	}
	
	fprintf(out,"\tli $v0, 10	#fin du programme\n\tsyscall\n");
}
	
void parsingInstruction(FILE * out, int indexGenMips)
{
	if(strcmp(tableGencode[indexGenMips]->commande,"addition") == 0)
	{
		codeAdditionGeneration(out, indexGenMips);
	}
	else if(strcmp(tableGencode[indexGenMips]->commande,"multiplication") == 0)
	{
		codeMultiplicationGeneration(out, indexGenMips);
	}
	else if(strcmp(tableGencode[indexGenMips]->commande,"moinsUnaire") == 0)
	{
		codeMoinsUnaireGeneration(out, indexGenMips);
	}
	else if(strcmp(tableGencode[indexGenMips]->commande,"affectation") == 0)
	{
		codeAffectationGeneration(out, indexGenMips);
	}
	else if(strcmp(tableGencode[indexGenMips]->commande,"affectationVal") == 0)
	{
		codeAffectationValGeneration(out, indexGenMips);
	}
	else if(strcmp(tableGencode[indexGenMips]->commande,"ecriture") == 0)
	{
		codeEcritureGeneration(out, indexGenMips);
	}
	else if(strcmp(tableGencode[indexGenMips]->commande,"ecritureVal") == 0)
	{
		codeEcritureValGeneration(out, indexGenMips);
	}
	else if(strcmp(tableGencode[indexGenMips]->commande,"chargeDansLaPile") == 0)
	{
		codeChargeDanslaPileGeneration(out, indexGenMips);
	}
	else if(strcmp(tableGencode[indexGenMips]->commande,"chargeDeLaPile") == 0)
	{
		codeChargeDeLaPileGeneration(out, indexGenMips);
	}
	else if(strcmp(tableGencode[indexGenMips]->commande,"logiqueNot") == 0)
	{
		codeNonLogiqueGeneration(out, indexGenMips);
	}
	else if(strcmp(tableGencode[indexGenMips]->commande,"logiqueOr") == 0)
	{
		codeOuLogiqueGeneration(out, indexGenMips);
	}
	else if(strcmp(tableGencode[indexGenMips]->commande,"logiqueXor") == 0)
	{
		codeXorLogiqueGeneration(out, indexGenMips);
	}
	else if(strcmp(tableGencode[indexGenMips]->commande,"logiqueAnd") == 0)
	{
		codeEtLogiqueGeneration(out, indexGenMips);
	}
	else if(strcmp(tableGencode[indexGenMips]->commande,"logiqueEgal") == 0)
	{
		codeEgalLogiqueGeneration(out, indexGenMips);
	}
	else if(strcmp(tableGencode[indexGenMips]->commande,"logiqueDifferent") == 0)
	{
		codeDifferentLogiqueGeneration(out, indexGenMips);
	}
	else if(strcmp(tableGencode[indexGenMips]->commande,"logiquePpetits") == 0)
	{
		codePpetitsLogiqueGeneration(out, indexGenMips);
	}
	else if(strcmp(tableGencode[indexGenMips]->commande,"logiquePpetit") == 0)
	{
		codePpetitLogiqueGeneration(out, indexGenMips);
	}
	else if(strcmp(tableGencode[indexGenMips]->commande,"logiquePgrands") == 0)
	{
		codePgrandsLogiqueGeneration(out, indexGenMips);
	}
	else if(strcmp(tableGencode[indexGenMips]->commande,"logiquePgrand") == 0)
	{
		codePgrandLogiqueGeneration(out, indexGenMips);
	}
	else if(strcmp(tableGencode[indexGenMips]->commande,"ecritureBool") == 0)
	{
		codeEcritureBoolGeneration(out, indexGenMips);
	}
	else if(strcmp(tableGencode[indexGenMips]->commande,"ecritureTrue") == 0)
	{
		codeEcritureVraiGeneration(out, indexGenMips);
	}
	else if(strcmp(tableGencode[indexGenMips]->commande,"ecriturFalse") == 0)
	{
		codeEcritureFauxGeneration(out, indexGenMips);
	}
	else if(strcmp(tableGencode[indexGenMips]->commande,"if") == 0)
	{
		codeIfGeneration(out, indexGenMips);
	}
	else if(strcmp(tableGencode[indexGenMips]->commande,"label") == 0)
	{
		codeLabelGeneration(out, indexGenMips);
	}
	else if(strcmp(tableGencode[indexGenMips]->commande,"jump") == 0)
	{
		codeJumpGeneration(out, indexGenMips);
	}
	else if(strcmp(tableGencode[indexGenMips]->commande,"chargerDeParametre") == 0)
	{
		codeChargerDeParametreGeneration(out, indexGenMips);
	}
	else if(strcmp(tableGencode[indexGenMips]->commande,"chargerDansParametre") == 0)
	{
		codeChargerDansParametreGeneration(out, indexGenMips);
	}
	else if(strcmp(tableGencode[indexGenMips]->commande,"sauveRA") == 0)
	{
		codeSauveRAGeneration(out, indexGenMips);
	}
	else if(strcmp(tableGencode[indexGenMips]->commande,"chargeRA") == 0)
	{
		codeChargeRAGeneration(out, indexGenMips);
	}
	else if(strcmp(tableGencode[indexGenMips]->commande,"appelFonction") == 0)
	{
		codeAppelFonctionGeneration(out, indexGenMips);
	}
	else if(strcmp(tableGencode[indexGenMips]->commande,"chargerResultatDans") == 0)
	{
		codeChargeResultatDansGeneration(out, indexGenMips);
	}
	else if(strcmp(tableGencode[indexGenMips]->commande,"return") == 0)
	{
		codeChargeResultatDeGeneration(out, indexGenMips);
	}
	else if(strcmp(tableGencode[indexGenMips]->commande,"readEntier") == 0)
	{
		codeReadEntierGeneration(out, indexGenMips);
	}
	else if(strcmp(tableGencode[indexGenMips]->commande,"readBooleen") == 0)
	{
		codeReadBooleenGeneration(out, indexGenMips);
	}
	else
	{
		printf("Erreur : parsingInstruction : commande inconnue (%s)\n",tableGencode[indexGenMips]->commande);
		exit(-1);
	}
}

void codeAdditionGeneration (FILE * out, int indexGenMips)
{
	char * retour = tableGencode[indexGenMips]->adresse1;
	char * source1 = tableGencode[indexGenMips]->adresse2;
	char * source2 = tableGencode[indexGenMips]->adresse3;
	
	fprintf(out,"\tlw $t0, %s	#%s := %s + %s\n",source1,retour,source1,source2);
	fprintf(out,"\tlw $t1, %s\n",source2);
	fprintf(out,"\tadd $t0, $t0, $t1\n");
	fprintf(out,"\tsw $t0, %s\n",retour);
	fprintf(out,"\n");
}

void codeMultiplicationGeneration (FILE * out, int indexGenMips)
{
	char * retour = tableGencode[indexGenMips]->adresse1;
	char * source1 = tableGencode[indexGenMips]->adresse2;
	char * source2 = tableGencode[indexGenMips]->adresse3;
	
	fprintf(out,"\tlw $t0, %s	#%s := %s * %s\n",source1,retour,source1,source2);
	fprintf(out,"\tlw $t1, %s\n",source2);
	fprintf(out,"\tmul $t0, $t0, $t1\n");
	fprintf(out,"\tsw $t0, %s\n",retour);
	fprintf(out,"\n");
}

void codeMoinsUnaireGeneration (FILE * out, int indexGenMips)
{
	char * retour = tableGencode[indexGenMips]->adresse1;
	char * source1 = tableGencode[indexGenMips]->adresse2;
	
	fprintf(out,"\tlw $t0, %s	#%s := - %s\n",source1,retour,source1);
	fprintf(out,"\tli $t1, -1\n");
	fprintf(out,"\tmul $t0, $t0, $t1\n");
	fprintf(out,"\tsw $t0, %s\n",retour);
	fprintf(out,"\n");
}

void codeAffectationGeneration (FILE * out, int indexGenMips)
{
	char * retour = tableGencode[indexGenMips]->adresse1;
	char * source = tableGencode[indexGenMips]->adresse2;
	
	fprintf(out,"\tlw $t0, %s	#%s := %s\n",source,retour,source);
	fprintf(out,"\tsw $t0, %s\n",retour);
	fprintf(out,"\n");
}

void codeAffectationValGeneration (FILE * out, int indexGenMips)
{
	char * retour = tableGencode[indexGenMips]->adresse1;
	char * source = tableGencode[indexGenMips]->adresse2;
	
	fprintf(out,"\tli $t0, %s	#%s := %s(int)\n",source,retour,source);
	fprintf(out,"\tsw $t0, %s\n",retour);
	fprintf(out,"\n");
}

void codeEcritureGeneration (FILE * out, int indexGenMips)
{
	char * source = tableGencode[indexGenMips]->adresse1;
	
	/*if(consulteHashTable(source).valeur.type == ENTREE_TYPECTE_CHAR)
	{
		fprintf(out,"\tli $v0,4		#print id_char\n");
		fprintf(out,"\tla $a0,%s\n",source);
		fprintf(out,"\tsyscall\n");
		fprintf(out,"\tli $v0,4\n");
		fprintf(out,"\tla $a0,sys_retour_ligne\n");
		fprintf(out,"\tsyscall\n");
		fprintf(out,"\n");
	}
	else if(consulteHashTable(source).valeur.type == ENTREE_TYPECTE_ENTIER)
	{*/
		fprintf(out,"\tli $v0,1		#print %s_int\n",source);
		fprintf(out,"\tlw $a0,%s\n",source);
		fprintf(out,"\tsyscall\n");
		fprintf(out,"\tli $v0,4\n");
		fprintf(out,"\tla $a0,sys_retour_ligne\n");
		fprintf(out,"\tsyscall\n");
		fprintf(out,"\n");
	/*}
	else
	{
		printf("Erreur : codeEcritureGeneration : type constante non reconnu.\n");
		exit(-1);
	}*/
}

void codeEcritureValGeneration (FILE * out, int indexGenMips)
{
	char * source = tableGencode[indexGenMips]->adresse1;

	fprintf(out,"\tli $v0,1		#print %s(int)\n",source);
	fprintf(out,"\tli $a0,%s\n",source);
	fprintf(out,"\tsyscall\n");
	fprintf(out,"\tli $v0,4\n");
	fprintf(out,"\tla $a0,sys_retour_ligne\n");
	fprintf(out,"\tsyscall\n");
	fprintf(out,"\n");
}

void codeChargeDanslaPileGeneration (FILE * out, int indexGenMips)
{
	char * source = tableGencode[indexGenMips]->adresse1;
	
	fprintf(out,"\tlw $t0, %s	#charge %s dans la pile\n",source,source);
	fprintf(out,"\tsw $t0, 0($sp)\n");
	fprintf(out,"\taddi $sp, $sp, 4\n");
	fprintf(out,"\n"); 
}

void codeChargeDeLaPileGeneration (FILE * out, int indexGenMips)
{
	char * destination = tableGencode[indexGenMips]->adresse1;
		
	fprintf(out,"\taddi $sp, $sp, -4	#charge dans %s la pile\n", destination);
	fprintf(out,"\tlw $t0, 0($sp)\n");
	fprintf(out,"\tsw $t0, %s\n",destination);
	fprintf(out,"\n"); 	
}

void codeNonLogiqueGeneration (FILE * out, int indexGenMips)
{
	char * retour = tableGencode[indexGenMips]->adresse1;
	char * source1 = tableGencode[indexGenMips]->adresse2;
	
	fprintf(out,"\tlw $a0, %s	#%s := not %s\n",source1,retour,source1);
	fprintf(out,"\tsw $ra, 0($sp)\n");
	fprintf(out,"\taddi $sp, $sp, 4\n");
	fprintf(out,"\tjal _SysLabelNon\n");
	fprintf(out,"\taddi $sp, $sp, -4\n");
	fprintf(out,"\tlw $ra, 0($sp)\n");
	fprintf(out,"\tsw $v1, %s\n",retour);
	fprintf(out,"\n"); 
	
}

void codeOuLogiqueGeneration (FILE * out, int indexGenMips)
{
	char * retour = tableGencode[indexGenMips]->adresse1;
	char * source1 = tableGencode[indexGenMips]->adresse2;
	char * source2 = tableGencode[indexGenMips]->adresse3;
	
	fprintf(out,"\tlw $a0, %s	#%s := %s ou %s\n",source1,retour,source1,source2);
	fprintf(out,"\tlw $a1, %s\n",source2);
	fprintf(out,"\tsw $ra, 0($sp)\n");
	fprintf(out,"\taddi $sp, $sp, 4\n");
	fprintf(out,"\tjal _SysLabelOu\n");
	fprintf(out,"\taddi $sp, $sp, -4\n");
	fprintf(out,"\tlw $ra, 0($sp)\n");
	fprintf(out,"\tsw $v1, %s\n",retour);
	fprintf(out,"\n");
}

void codeEtLogiqueGeneration (FILE * out, int indexGenMips)
{
	char * retour = tableGencode[indexGenMips]->adresse1;
	char * source1 = tableGencode[indexGenMips]->adresse2;
	char * source2 = tableGencode[indexGenMips]->adresse3;
	
	fprintf(out,"\tlw $a0, %s	#%s := %s et %s\n",source1,retour,source1,source2);
	fprintf(out,"\tlw $a1, %s\n",source2);
	fprintf(out,"\tsw $ra, 0($sp)\n");
	fprintf(out,"\taddi $sp, $sp, 4\n");
	fprintf(out,"\tjal _SysLabelEt\n");
	fprintf(out,"\taddi $sp, $sp, -4\n");
	fprintf(out,"\tlw $ra, 0($sp)\n");
	fprintf(out,"\tsw $v1, %s\n",retour);
	fprintf(out,"\n");
}

void codeXorLogiqueGeneration (FILE * out, int indexGenMips)
{
	char * retour = tableGencode[indexGenMips]->adresse1;
	char * source1 = tableGencode[indexGenMips]->adresse2;
	char * source2 = tableGencode[indexGenMips]->adresse3;
	
	fprintf(out,"\tlw $a0, %s	#%s := %s xor %s\n",source1,retour,source1,source2);
	fprintf(out,"\tlw $a1, %s\n",source2);
	fprintf(out,"\tsw $ra, 0($sp)\n");
	fprintf(out,"\taddi $sp, $sp, 4\n");
	fprintf(out,"\tjal _SysLabelXor\n");
	fprintf(out,"\taddi $sp, $sp, -4\n");
	fprintf(out,"\tlw $ra, 0($sp)\n");
	fprintf(out,"\tsw $v1, %s\n",retour);
	fprintf(out,"\n");
}

void codeEgalLogiqueGeneration (FILE * out, int indexGenMips)
{
	char * retour = tableGencode[indexGenMips]->adresse1;
	char * source1 = tableGencode[indexGenMips]->adresse2;
	char * source2 = tableGencode[indexGenMips]->adresse3;
	
	fprintf(out,"\tlw $t0, %s	#%s := %s = %s\n",source1,retour,source1,source2);
	fprintf(out,"\tlw $t1, %s\n",source2);
	fprintf(out,"\tseq $t0, $t0, $t1\n");
	fprintf(out,"\tsw $t0, %s\n",retour);
	fprintf(out,"\n");
}

void codeDifferentLogiqueGeneration (FILE * out, int indexGenMips)
{
	char * retour = tableGencode[indexGenMips]->adresse1;
	char * source1 = tableGencode[indexGenMips]->adresse2;
	char * source2 = tableGencode[indexGenMips]->adresse3;
	
	fprintf(out,"\tlw $t0, %s	#%s := %s <> %s\n",source1,retour,source1,source2);
	fprintf(out,"\tlw $t1, %s\n",source2);
	fprintf(out,"\tsne $t0, $t0, $t1\n");
	fprintf(out,"\tsw $t0, %s\n",retour);
	fprintf(out,"\n");
}

void codePpetitLogiqueGeneration (FILE * out, int indexGenMips)
{
	char * retour = tableGencode[indexGenMips]->adresse1;
	char * source1 = tableGencode[indexGenMips]->adresse2;
	char * source2 = tableGencode[indexGenMips]->adresse3;
	
	fprintf(out,"\tlw $t0, %s	#%s := %s <= %s\n",source1,retour,source1,source2);
	fprintf(out,"\tlw $t1, %s\n",source2);
	fprintf(out,"\tsle $t0, $t0, $t1\n");
	fprintf(out,"\tsw $t0, %s\n",retour);
	fprintf(out,"\n");
}

void codePpetitsLogiqueGeneration (FILE * out, int indexGenMips)
{
	char * retour = tableGencode[indexGenMips]->adresse1;
	char * source1 = tableGencode[indexGenMips]->adresse2;
	char * source2 = tableGencode[indexGenMips]->adresse3;
	
	fprintf(out,"\tlw $t0, %s	#%s := %s < %s\n",source1,retour,source1,source2);
	fprintf(out,"\tlw $t1, %s\n",source2);
	fprintf(out,"\tslt $t0, $t0, $t1\n");
	fprintf(out,"\tsw $t0, %s\n",retour);
	fprintf(out,"\n");
}

void codePgrandLogiqueGeneration (FILE * out, int indexGenMips)
{
	char * retour = tableGencode[indexGenMips]->adresse1;
	char * source1 = tableGencode[indexGenMips]->adresse2;
	char * source2 = tableGencode[indexGenMips]->adresse3;
	
	fprintf(out,"\tlw $t0, %s	#%s := %s >= %s\n",source1,retour,source1,source2);
	fprintf(out,"\tlw $t1, %s\n",source2);
	fprintf(out,"\tsge $t0, $t0, $t1\n");
	fprintf(out,"\tsw $t0, %s\n",retour);
	fprintf(out,"\n");
}

void codePgrandsLogiqueGeneration (FILE * out, int indexGenMips)
{
	char * retour = tableGencode[indexGenMips]->adresse1;
	char * source1 = tableGencode[indexGenMips]->adresse2;
	char * source2 = tableGencode[indexGenMips]->adresse3;
	
	fprintf(out,"\tlw $t0, %s	#%s := %s > %s\n",source1,retour,source1,source2);
	fprintf(out,"\tlw $t1, %s\n",source2);
	fprintf(out,"\tsgt $t0, $t0, $t1\n");
	fprintf(out,"\tsw $t0, %s\n",retour);
	fprintf(out,"\n");
}

void codeEcritureBoolGeneration (FILE * out, int indexGenMips)
{
	char * valeur = tableGencode[indexGenMips]->adresse1;
	
	int x = nbLabelPourGenMips;
	
	fprintf(out,"\tlw $t0, %s	#print %s_bool\n",valeur,valeur);
	fprintf(out,"\tbeqz $t0, _SysLabelGenMips%d\n",x);	
	fprintf(out,"\tli $v0,4\n");
	fprintf(out,"\tla $a0,sys_vrai\n");
	fprintf(out,"\tsyscall\n");
	fprintf(out,"\tb _SysLabelGenMips%d\n",x+1);
	fprintf(out,"\t_SysLabelGenMips%d :\n",x);
	fprintf(out,"\tli $v0,4\n");
	fprintf(out,"\tla $a0,sys_faux\n");
	fprintf(out,"\tsyscall\n");
	fprintf(out,"\t_SysLabelGenMips%d :\n",x+1);
	fprintf(out,"\tli $v0,4\n");
	fprintf(out,"\tla $a0,sys_retour_ligne\n");
	fprintf(out,"\tsyscall\n");
	fprintf(out,"\n");
	
	nbLabelPourGenMips += 2;
}

void codeEcritureVraiGeneration (FILE * out, int indexGenMips)
{
	char * valeur = tableGencode[indexGenMips]->adresse1; // pour virer le warning et garder un prototype standard.
	if(valeur == NULL)
	{}

	fprintf(out,"\tli $v0,4		#print vrai\n");
	fprintf(out,"\tla $a0,sys_vrai\n");
	fprintf(out,"\tsyscall\n");
	fprintf(out,"\tli $v0,4\n");
	fprintf(out,"\tla $a0,sys_retour_ligne\n");
	fprintf(out,"\tsyscall\n");
	fprintf(out,"\n");
}

void codeEcritureFauxGeneration (FILE * out, int indexGenMips)
{
	char * valeur = tableGencode[indexGenMips]->adresse1; // pour virer le warning et garder un prototype standard.
	if(valeur == NULL)
	{}

	fprintf(out,"\tli $v0,4		#print faux\n");
	fprintf(out,"\tla $a0,sys_faux\n");
	fprintf(out,"\tsyscall\n");
	fprintf(out,"\tli $v0,4\n");
	fprintf(out,"\tla $a0,sys_retour_ligne\n");
	fprintf(out,"\tsyscall\n");
	fprintf(out,"\n");
}

void codeIfGeneration (FILE * out, int indexGenMips)
{
	char * id = tableGencode[indexGenMips]->adresse1;
	char * label = tableGencode[indexGenMips]->adresse2;
	
	fprintf(out,"\tlw $t0, %s		#if %s = 0 goto %s\n",id,id,label);
	fprintf(out,"\tbeqz $t0, %s\n",label);
	fprintf(out,"\n");
}

void codeLabelGeneration (FILE * out, int indexGenMips)
{
	char * label = tableGencode[indexGenMips]->adresse1;
	
	fprintf(out,"%s :		#label %s :\n",label,label);
	fprintf(out,"\n");
}

void codeJumpGeneration (FILE * out, int indexGenMips)
{
	char * label = tableGencode[indexGenMips]->adresse1;
	
	fprintf(out,"b %s		#goto %s\n",label,label);
	fprintf(out,"\n");
}

void codeChargerDeParametreGeneration(FILE * out, int indexGenMips)
{
	char * destination = tableGencode[indexGenMips]->adresse1;
	char * argument = tableGencode[indexGenMips]->adresse2;
	
	fprintf(out,"\tsw %s, %s	#%s := %s\n",argument,destination,destination,argument);
	fprintf(out,"\n");
}

void codeChargerDansParametreGeneration(FILE * out, int indexGenMips)
{
	char * source = tableGencode[indexGenMips]->adresse1;
	char * argument = tableGencode[indexGenMips]->adresse2;
	
	fprintf(out,"\tlw %s, %s	#%s := %s\n",argument,source,argument,source);
	fprintf(out,"\n");
}

void codeSauveRAGeneration(FILE * out, int indexGenMips)
{	
	char * valeur = tableGencode[indexGenMips]->adresse1; // pour virer le warning et garder un prototype standard.
	if(valeur == NULL)
	{}

	fprintf(out,"\tsw $ra, 0($sp)	#charge ra dans la pile\n");
	fprintf(out,"\taddi $sp, $sp, 4\n");
	fprintf(out,"\n");
}

void codeChargeRAGeneration(FILE * out, int indexGenMips)
{
	char * valeur = tableGencode[indexGenMips]->adresse1; // pour virer le warning et garder un prototype standard.
	if(valeur == NULL)
	{}

	fprintf(out,"\taddi $sp, $sp, -4	#charge ra a partir de la pile\n");
	fprintf(out,"\tlw $ra, 0($sp)\n");
	fprintf(out,"\n");
}

void codeAppelFonctionGeneration(FILE * out, int indexGenMips)
{
	char * label = tableGencode[indexGenMips]->adresse1;
	
	fprintf(out,"\tjal %s	#appel la fonction %s\n",label,label);
	fprintf(out,"\n");
}

void codeChargeResultatDansGeneration(FILE * out, int indexGenMips)	
{
	char * destination = tableGencode[indexGenMips]->adresse1;
	
	fprintf(out,"\tsw $v1, %s	#%s := $v1\n",destination,destination);
	fprintf(out,"\n");
}

void codeChargeResultatDeGeneration(FILE * out, int indexGenMips)
{
	char * source = tableGencode[indexGenMips]->adresse1;
	
	fprintf(out,"\tlw $v1, %s	#$v1 := %s\n",source,source);
	fprintf(out,"\n");
}

void codeReadEntierGeneration(FILE * out, int indexGenMips)
{
	char * destination = tableGencode[indexGenMips]->adresse1;
	
	fprintf(out,"\tli $v0,5 #lit un entier et le met dans %s\n", destination);
	fprintf(out,"\tla $a0,sys_retour_ligne\n");
	fprintf(out,"\tsyscall\n");
	fprintf(out,"\tsw $v0, %s\n",destination);
	fprintf(out,"\n");
}


void codeReadBooleenGeneration(FILE * out, int indexGenMips)
{
	char * destination = tableGencode[indexGenMips]->adresse1;
	
	fprintf(out,"\tli $v0,5 #lit un entier et si c'est 0 le met dans %s sinon met 1 dans %s\n", destination,destination);
	fprintf(out,"\tla $a0,sys_retour_ligne\n");
	fprintf(out,"\tsyscall\n");
	fprintf(out,"\tli $t0, 0\n");
	fprintf(out,"\tsne $v0, $v0, $t0\n");
	fprintf(out,"\tsw $v0, %s\n",destination);
	fprintf(out,"\n");
}

